<template>
<div class="login-bg">
  <el-row>
    <el-col :span="8" :offset="8">
      <el-form ref="loginUser" :model="loginUser" label-width="80px" class="login-form">
        <h2>用户管理系统</h2>
        <el-form-item label="用户名">
          <el-input v-model="loginUser.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="loginUser.password" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">登录</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>

</div>
</template>

<script>
import axios from "axios";

export default {
  name: "LoginView",
  data() {
    return {
      loginUser:{
        username:"admin",
        password:"123456"
      }
    }
  },
  methods: {
    onSubmit(){
      console.log(this.loginUser)
      axios.post("/user/login",this.loginUser)
          .then(res=>{
            if(res.code==2000){
              this.$message.success("登录成功")
              //把登录信息存储到vuex中
              this.$store.commit("setusername",this.loginUser.username)
              //登陆成功后将token存入vuex中
              this.$store.commit("setToken",res.data)
              //登录成功后跳转主页
              this.$router.push("/admin")
            }else{
              console.log(res.code)
              this.$message.error("登录失败")
            }

          })
          .catch(error=>{
            console.error( error)
          })
    }
  }
}
</script>

<style scoped>
.login-bg{
  background-image: url("../assets/壁纸3.jpg");
  background-size: cover;
  height: 100vh;
}
.login-form{
  margin-top: 40%;
  background-color: white;
  padding: 35px;
}

</style>